package jh.com.jhappbf.ui;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.ab.view.sliding.AbSlidingTabView;
import com.ab.view.slidingmenu.SlidingMenu;
import com.ab.view.titlebar.AbTitleBar;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import jh.com.jhappbf.R;
import jh.com.jhappbf.unload.LoadManager;
import jh.com.jhappbf.usermanager.UserManagerMain;
import jh.com.jhappbf.weight.collect.CollectMain;
import jh.com.jhappbf.weight.query.QueryMain;
import jh.com.jhappbf.yuntuUnload.BuyUnloadManager;
import jh.com.jhappbf.yuntuUnload.SaleUnloadManager;
import jh.com.jhappbf.yuntuUnload.SpareUnloadManager;

/**
 * @author Administrator
 */
public class Main extends AbActivity {
    SlidingMenu mLeftMenu;
    SlidingMenuFragment mMenuFragment = null;
    private long mTimeInterval = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.main_activity);

        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText(R.string.main_top);
        mAbTitleBar.setLogo(R.drawable.button_selector_menu);
//		mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this,R.color.orange));
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        mAbTitleBar.getLogoView().setOnClickListener(v -> {
            if (mLeftMenu.isMenuShowing()) {
                mLeftMenu.showContent();
            } else {
                mLeftMenu.showMenu();
            }
        });

        //初始化左边的菜单项
        mLeftMenu = new SlidingMenu(this);
        mLeftMenu.setMode(SlidingMenu.LEFT);
        mLeftMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        mLeftMenu.setShadowWidthRes(R.dimen.slidting_shadow_width);
        mLeftMenu.setShadowDrawable(R.drawable.left_sliding_menu_shadow);
        mLeftMenu.setFadeDegree(0.35f);
        mLeftMenu.setBehindOffsetRes(R.dimen.sliding_menu_offset);
        mLeftMenu.setSlidingEnabled(false);
        mLeftMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

        mLeftMenu.setMenu(R.layout.layout_frame);
        mMenuFragment = new SlidingMenuFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, mMenuFragment).commit();

        AbSlidingTabView mTopTabView = findViewById(R.id.main_topTapView);
        // 缓存数量
        mTopTabView.getViewPager().setOffscreenPageLimit(5);

        // 设置样式
        mTopTabView.setTabTextColor(Color.BLACK);
        mTopTabView.setTabSelectColor(Color.rgb(30, 168, 131));
        mTopTabView.setTabBackgroundResource(R.drawable.tab_bg);
        mTopTabView.setTabLayoutBackgroundResource(R.mipmap.slide_top);

        // 演示增加一组
        List<String> mList = new ArrayList<>();
        List<Fragment> mFragments = new ArrayList<>();

        if (State.mRight.contains("weight") ||State.mRight.contains("admin") || "gys".equals(State.mAccount)) {
            mList.add("称重查询");
            mFragments.add(new QueryMain());
        }

        if (State.mRight.contains("collect") ||State.mRight.contains("admin")|| "gys".equals(State.mAccount)) {
            mList.add("称重汇总");
            mFragments.add(new CollectMain());
        }

        if (State.mRight.contains("saleOrder") ||State.mRight.contains("admin")|| "gys".equals(State.mAccount)) {
            mList.add("交货单确认");
            mFragments.add(new SaleUnloadManager());
        }

        if (State.mRight.contains("buyOrder") ||State.mRight.contains("admin")|| "gys".equals(State.mAccount)) {
            mList.add("采购单确认");
            mFragments.add(new BuyUnloadManager());
        }
//
        if (State.mRight.contains("spareOrder") ||State.mRight.contains("admin")|| "gys".equals(State.mAccount)) {
            mList.add("预留单确认");
            mFragments.add(new SpareUnloadManager());
        }


//		mList.add("状态管理");
//		mFragments.add(new UnloadManager());  receipt
//        if (State.mRight.contains("dispatch") || "gys".equals(State.mAccount)) {
//            mList.add("派车管理");
//            mFragments.add(new DispatchCarManager());
//        }
//        if (State.mRight.contains("receipt") || "gys".equals(State.mAccount)) {
//            mList.add("磅单管理");
//            mFragments.add(new ReceiptManager());
//        }
//        if (State.mRight.contains("data") || "gys".equals(State.mAccount)) {
//            mList.add("数据管理");
//            mFragments.add(new DataManager());
//        }
//
//
//        if (State.mRight.contains("assay") || "gys".equals(State.mAccount)) {
//            mList.add("化验管理");
//            mFragments.add(new AssayManager());
//        }
////
//        if (State.mRight.contains("finance") || "gys".equals(State.mAccount)) {
//            mList.add("财务管理");
//            mFragments.add(new FinanceMain());
//        }
//
////
//        if (State.mRight.contains("load") || "gys".equals(State.mAccount)) {
//            mList.add("确认装卸货");
//            mFragments.add(new UnloadManager());
//        }

        if (State.mRight.contains("unload") || "gys".equals(State.mAccount)) {
            mList.add("确认查询");
            mFragments.add(new LoadManager());
        }



//        if (State.mRight.contains("verify")|| "gys".equals(State.mAccount)) {
//            mList.add("审核管理");
//            mFragments.add(new VerifyManager());
//        }
//
//        if (State.mRight.contains("plan")|| "gys".equals(State.mAccount)) {
//            mList.add("计划管理");
//            mFragments.add(new ContractManager());
//        }

        if (State.mRight.contains("manager") ||State.mRight.contains("admin")|| "gys".equals(State.mAccount)) {
            mList.add("用户管理");
            mFragments.add(new UserManagerMain());
        }

        mTopTabView.addItemViews(mList, mFragments);
        mTopTabView.setTabPadding(20, 8, 20, 8);
    }

    @SuppressLint("RestrictedApi")
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - mTimeInterval > 2000) {
                Toast.makeText(this, "再按一次退出系统！", Toast.LENGTH_SHORT).show();
                mTimeInterval = System.currentTimeMillis();
                return false;
            } else {
                this.finish();
            }
        }
        return super.dispatchKeyEvent(event);
    }

}